<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');}

class Profile_Model {
	var $DB;
	function Profile_Model ( $DB ){
		$this->DB = $DB;
	}

	function getAdminUser($adminuser){
		$sql = "SELECT * FROM admin WHERE adminuser = '$adminuser' ";
		$this->DB->query($sql);		
		return $this->DB->fetchRow();
	}

	function getId($id){
		$sql = "SELECT * FROM admin WHERE id = '$id' ";
		$this->DB->query($sql);		
		return $this->DB->fetchRow();
	}

	function getList($condition){
		$sql = "SELECT * FROM admin ";
		$tmp = "";
		if(!empty($condition['condition'])){
			$tmp .= "WHERE {$condition['condition']} ";
		}
		if(!empty($condition['order'])){
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		}
		if(!empty($condition['page']) && !empty($condition['rows'])){
			$start = ($condition['page'] - 1) * $condition['rows'];
			$tmp .= "LIMIT " . $start . ", " . $condition['rows'];
		}
		$sql .= $tmp;
		//de($sql);
		$this->DB->query($sql);		
		return $this->DB->fetchAll();	
	}
	function total($condition){
		$sql = "SELECT count(*) AS total FROM admin ";
		$tmp = "";
		if(!empty($condition['conditions'])){
			$tmp .= "WHERE {$condition['conditions']} ";
		}
		if(!empty($condition['order'])){
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		}
		$sql .= $tmp;
		//de($sql);
		$this->DB->query($sql);
		$this->DB->fetchRow();
		return $this->DB->getValue("total");
	}

	function addUser($adminuser,$password){
		global $timestamp,$onlineip;
		$password = md5($password);
		$sql = "INSERT INTO admin (adminuser,password,loginip,logintime) VALUES ('$adminuser','$password','$onlineip','$timestamp')";
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}

	function editUser($id,$adminuser,$password){		
		$sql = "UPDATE admin SET adminuser = '$adminuser',password = '$password' WHERE id = '$id' ";
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}

	function delUser($userid){
		$sql = "DELETE FROM admin WHERE id = '$userid'" ;
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}
}

?>
